package Offer34;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 23737
 * @time 2021.9.17
 *  二叉树中和为某一值的路径:输入一棵二叉树和一个整数，打印出二叉树中节点值的和为输入整数的所有路径。
 *  从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。
 */
public class Test {
    public static void main(String[] args) {

    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {
    }
    TreeNode(int val) {
        this.val = val;
    }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

/*做不出来看题解系列*/
class Solution {
    Deque<Integer> deque = new LinkedList<>();
    List<List<Integer>> list = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        dfs(root,target);
        return list;
    }

    private void dfs(TreeNode root, int target) {
        if (root == null) return;
        deque.offerLast(root.val);
        target -= root.val;
        if(root.left == null && root.right==null && target == 0){
            list.add(new LinkedList<Integer>(deque));
        }
        dfs(root.left,target);
        dfs(root.right,target);
        deque.pollLast();
    }
}


